thin.imports('/public/configure/conf.sms.js');

var __getdata_send = {},
	__getdata_check = {};

function verifyGET() {
	var that = $(this),
		mobile = $('[layout-mark=sms-mobile]').val();
	if(mobile == '') {
		$.toptip('请输入手机号', 'error');
		return;
	}
	var scene = $(this).attr('data-scene');
	__getdata_send = {
		'scene': scene,
		'mobile': mobile
	}
	factory.request('_sms_send', that);
}

function verifyCD(obj, time) {
	obj.html(time + 's');
	if(time == 0) {
		obj.html('获取验证码');
		obj.on('click', verifyGET);
		obj.removeClass('weui-btn_disabled');
		return null;
	}
	setTimeout(function() {
		verifyCD(obj, --time);
	}, 1000);
}

function __complete_send(res, unkn) {
	if(res.status == 200) {
		$.toast('验证码已发送', 'text');

		// 解绑
		unkn.off('click');
		$(document).off('click', '[layout-mark=sms-get]');

		unkn.addClass('weui-btn_disabled');
		verifyCD(unkn, 60);
		if(res.data == 'register' || res.data == 'bind') {
			$('#btn-checkaccess').data('action', res.data);
		}
	} else {
		$.toptip(res.error, 'error');
	}
}

function __complete_check(res, unkn) {
	if(res.status == 201) {
		unkn();
	} else {
		$.hideLoading();
		$.toptip(res.error, 'error');
	}
}

function verifySMSCode(callback) {
	var mobile = $('[layout-mark=sms-mobile]').val(),
		code = $('[layout-mark=sms-code]').val();
	if(mobile == '') {
		$.toptip('请输入手机号', 'error');
		return;
	} else if(code == '') {
		$.toptip('请输入验证码', 'error');
		return;
	}
	$.showLoading();
	__getdata_check = {
		'mobile': mobile,
		'code': code
	};
	factory.request('_sms_check', callback);
}

$(function() {
	//$('#sendsms').on('click', verifyGET);
	$(document).on('click', '[layout-mark=sms-get]', verifyGET);
});